import { useEffect, useState } from "react";

function useDebounce(val: string, delay: number) {
  const [value, setValue] = useState(val);
  useEffect(() => {
    const timer = window.setTimeout(() => {
      setValue(val);
    }, delay);
    return () => {
      clearTimeout(timer);
    };
  }, [val]);

  return value;
}

function useDebounce2(delay: number, value: number) {
  const [state, setState] = useState<number>();

  useEffect(() => {
    const timer = window.setTimeout(() => {
      setState(value);
    }, delay);
    return () => {
      clearTimeout(timer);
    };
  }, [value, delay]);

  return state;
}
export default useDebounce;
